
Partial Class Budget_ExpenditureReportDownload
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            UserAuthorization.CheckPagePermission(Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseMonthlyStatement, "VIEW")
            PopulateSpendingPlanList()
        End If

        EntityController.SetSubNavTabsFinancial(Me.EditCaseSubNav2)
        EntityController.SetFinancialTab(Me.FinancialSubName, Me.EditCaseSubNav2)
        lblMonthlyStatement.Text = EntityController.ReplaceDisplayName("Monthly Statement")
        MonthlyStatementsLabel.Text = "Approved " + EntityController.ReplaceDisplayName("Spending Plan") + "s:"
        EntityController.SetFinancialTab(Me.FinancialSubName, Me.EditCaseSubNav2)



        If Page.IsPostBack Then
            Dim spendingPlanId As Integer = Convert.ToInt32(ApprovedSpendingPlansDropDownList.SelectedValue)
            Dim Participant As New Cdm.DataCore.Entities.Participant()
            Participant = EntityController.GetParticipant()
            If Participant Is Nothing Then
                lblResult.Text = "There was a problem retrieving the " + EntityController.ReplaceDisplayName("Monthly Statement")
                lblResult.Visible = True
            Else
                Dim participantId As Integer = Participant.ParticipantId
                Dim template As String = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings.Get("TemplateLocation"))
                Dim filename As String
                Dim approvedPlan As Cdm.DataCore.Budget.ApprovedSpendingPlan = Cdm.DataCore.Budget.ApprovedSpendingPlan.Retrieve(spendingPlanId)
                Dim expBuilder As New Spreadsheet.ExpenditureReport()
                filename = expBuilder.DownloadExpenditureReport(approvedPlan, template, EntityController.ReplaceDisplayName("Monthly Statement"))
                If filename = "" Then
                    lblResult.Text = "There was a problem retrieving the " + EntityController.ReplaceDisplayName("Monthly Statement") + ". Please verify that the Participant has a current Budget."
                    lblResult.Visible = True
                Else
                    lblResult.Visible = False
                    Response.Clear()
                    Response.ContentType = "application/vnd.ms-excel"
                    Response.AddHeader("Content-Disposition", "attachment;filename=" & filename)
                    Response.AddHeader("Target", "newwindow")
                    Response.WriteFile(filename)
                End If
            End If
        End If

    End Sub

    Protected Sub EditCaseSubNav2_ChangeMenu(ByVal sender As Object, ByVal e As System.EventArgs) Handles EditCaseSubNav2.ChangeMenu
        EntityController.ReturnToCasePersonalPage(EditCaseSubNav2)
    End Sub

    Protected Sub PopulateSpendingPlanList()
        Dim participant As Cdm.DataCore.Entities.Participant = EntityController.GetParticipant()
        ApprovedSpendingPlansDropDownList.DataSource = Cdm.DataCore.Budget.ApprovedSpendingPlan.ListByParticipant(participant.ParticipantId)
        ApprovedSpendingPlansDropDownList.DataTextField = "DateRangeDescription"
        ApprovedSpendingPlansDropDownList.DataValueField = "SpendingPlanId"
        ApprovedSpendingPlansDropDownList.DataBind()

        Button1.Enabled = ApprovedSpendingPlansDropDownList.Items.Count > 0

    End Sub

    Private Function GetMonthListItem(ByVal monthsfromNow As Integer) As ListItem
        Dim dt As DateTime = DateAdd(DateInterval.Month, monthsfromNow, Now)
        Dim itemString As String = dt.Month.ToString & "/" & dt.Year.ToString()
        Return New ListItem(itemString, itemString)
    End Function

End Class
